/*
 * mail.cpp
 *
 *  Created on: Dec 29, 2013
 *      Author: killerg
 */


/*
appId 			必须 	游戏ID
cpOrderId 		必须 	开发商订单ID
cpUserInfo 		可选 	开发商透传信息
uid 			必须 	用户ID
orderId 		必须 	游戏平台订单ID
orderStatus 	必须 	订单状态，TRADE_SUCCESS 代表成功
payFee 	必须 	支付金额,单位为分,即0.01 米币。
productCode 	必须 	商品代码
productName 	必须 	商品名称
productCount 	必须 	商品数量
payTime 		必须 	支付时间,格式 yyyy-MM-dd HH:mm:ss
orderConsumeType可选 	订单类型：10：普通订单 11：直充直消订单
signature 		必须 	签名,签名方法见后面说明
 */

#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <sstream>
#include <string>

#include <event2/http.h>

#include "share/http_c3.h"
#include "share/json/json.h"
#include "share/util.h"


http_client3 httpc;

int main(int argc, char** argv) {
	if (argc < 4) {
		printf("usage: cmd fee account serverid\n");
		return -1;
	}

	std::string cfg_path = fmt_cstr("%s/bin/billclient.cfg", get_deploy());
	Json::Value cfg;
	const char* cfg_file = load_file(cfg_path.c_str());
	if (NULL != cfg_file)
		cfg = to_json(cfg_file);

	int cpOrderId = cfg.get("cpOrderId", 0).asInt() + 1;
	std::string appKey = cfg.get("appKey", "hello").asCString();
	std::string appId = cfg.get("appId", "123").asCString();
	std::string url = cfg.get("url", "http://xone:8080/bill/uc").asCString();
	std::string orderConsumeType = cfg.get("orderConsumeType", "10").asCString();
	int orderId = cfg.get("orderId", 0).asInt() + 1;

	time_t now = time(NULL);
	tm* t = localtime(&now);
	char buff[50];
	strftime(buff, 50, "%Y-%m-%d %H:%M:%S", t);

	if (!httpc.init()) {
		printf("httpc init err\n");
		return -1;
	}

	Json::Value userinfo;
	userinfo["serverid"] = atoi(argv[3]);							//*3

	bool fin = false;

    std::stringstream ss2;
    ss2 << "appId=" << appId.c_str();
    ss2 << "&cpOrderId=" << cpOrderId;
    ss2 << "&cpUserInfo=" << to_cstr(userinfo);
    ss2 << "&orderConsumeType=" << orderConsumeType;
    ss2 << "&orderId=" << orderId;
    ss2 << "&orderStatus=" << "TRADE_SUCCESS";
    ss2 << "&payFee=" << atoi(argv[1]);                          //*1
    ss2 << "&payTime=" << buff;
    ss2 << "&productCode=" << -1;
    ss2 << "&productCount=" << 0;
    ss2 << "&productName=" << "none";
    ss2 << "&uid=" << argv[2];                           //*2

	std::stringstream ss;
	ss << "appId=" << encode(appId.c_str());
	ss << "&cpOrderId=" << cpOrderId;
	ss << "&cpUserInfo=" << encode(to_cstr(userinfo));
	ss << "&orderConsumeType=" << orderConsumeType;
	ss << "&orderId=" << orderId;
	ss << "&orderStatus=" << "TRADE_SUCCESS";
	ss << "&payFee=" << atoi(argv[1]);							//*1
	ss << "&payTime=" << encode(buff);
	ss << "&productCode=" << -1;
	ss << "&productCount=" << 0;
	ss << "&productName=" << "none";
	ss << "&uid=" << encode(argv[2]);							//*2

	std::string signature = Sha1(ss2.str().c_str(), appKey.c_str());

	std::string uri = fmt_cstr("%s?%s&signature=%s", url.c_str(), ss.str().c_str(), signature.c_str());

	printf("%s\n", uri.c_str());
	httpc.do_query(uri.c_str(), NULL, [&fin](int code, const std::string& data){
		fin = true;
		Json::Value ret = to_json(data);
		switch (ret["errcode"].asInt()) {
		case 200:
			printf("验证正确\n");
			break;
		case 1515:
            printf("%s\n", ret["errMsg"].asCString());
			break;
		case 1516:
			printf("%s\n", ret["errMsg"].asCString());
			break;
		case 1520:
            printf("%s\n", ret["errMsg"].asCString());
			break;
		case 1525:
            printf("%s\n", ret["errMsg"].asCString());
			break;
		}
		int errcode = ret["errcode"].asInt();
		printf("%d\n", errcode);
	});

	for (int i=0; i<10 && !fin; ++i) {
		httpc.update();
		sleep(1);
	}

	if (!fin) {
		printf("timeout\n");
	}

	httpc.destroy();

	cfg["orderConsumeType"] = orderConsumeType;
	cfg["cpOrderId"] = cpOrderId;
	cfg["orderId"] = orderId;
	cfg["appKey"] = appKey;
	cfg["appId"] = appId;
	cfg["url"] = url;

	save_file(cfg_path.c_str(), to_cstr(cfg));
}
